// pages/detail/detail.js
const {
  playData
} = require("../../utils/util")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeName: 0,
    videoUrl: "",
    dataList: {},
    isLogin: false, //是否登录
    isMember: false, //是否是会员
    isBuy: false, //是否购买
  },
  onLoad() {
    this.initData();
  },
  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
  },
  //点击添加显示播放字体
  highlight(event) {
    if (!this.data.isLogin) {
      wx.switchTab({
        url: '/pages/my/my',
      })
      return;
    } else if (!this.data.isLogin || !this.data.isMember && !this.data.isBuy) {
      this.onSubmit();
      return;
    }
    const index = event.currentTarget.dataset.index;
    const newDataList = this.data.dataList;
    this.setData({
      videoUrl: "",
    })
    for (var i = 0; i < newDataList.videoList.length; i++) {
      let newContentList = newDataList.videoList[i].contentList;
      for (var j = 0; j < newContentList.length; j++) {
        newContentList[j].showTail = false;
        if (newContentList[j].id == index) {
          newContentList[j].showTail = true;
          newDataList.videoList[i].contentList[j] = newContentList[j];
        }
      }
    }
    this.setData({
      dataList: newDataList,
      videoUrl: event.currentTarget.dataset.videoUrl
    })
  },
  initData() {
    const user = wx.getStorageSync("user");
    if (user == null || Object.keys(user).length === 0) {
      this.setData({
        isLogin: false
      })
    } else {
      this.setData({
        isLogin: true,
        isMember: user.isMember
      })
    }
    const data = playData;
    this.setData({
      dataList: data,
      videoUrl: data.videoList[0].contentList[0].url
    })
  },
  onSubmit() {
    if (!this.data.isLogin) {
      wx.switchTab({
        url: '/pages/my/my',
      })
      return;
    }
    const thisData = this.data.dataList;
    wx.navigateTo({
      url: '/pages/pay/pay?goodId=' + thisData.id,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: thisData.id
        })
      }
    })
  }
})